package com.cxs.xiaomi.dao.impl;

import com.cxs.xiaomi.dao.CartDao;
import com.cxs.xiaomi.domain.Cart;
import cxs.utils.handler.impl.ResultHandlerAsBeanImpl;
import cxs.utils.handler.impl.ResultHandlerAsListImpl;
import cxs.utils.templete.JdbcTemplete;

import java.util.List;

/*
 * @Author:cxs
 * @Date:2021/9/15  17:09
 * @Version 1.0
 * @Motto:放下杂念,只为迎接明天更好的自己
 * */
public class CartDaoImpl implements CartDao {
    @Override
    public int insert(Cart cart) {
        String sql = "insert into tb_cart values(?,?,?,?)";
        return JdbcTemplete.DMLTemplete(sql,cart.getId(),cart.getPid(),cart.getNum(),cart.getMoney());
    }

    @Override
    public Cart selectByUnionPrimary(Integer userId, Integer goodId) {
        String sql = "select * from tb_cart where id = ? and pid = ?";
        return JdbcTemplete.DQLTemplete(sql,new ResultHandlerAsBeanImpl<>(Cart.class),userId,goodId);
    }

    @Override
    public int updateByUnionPrimary(Cart cartShop) {
        String sql = "update tb_cart set Num = ?,money = ? where id = ? and pid = ?";
        Object[] arrs = {cartShop.getNum(),cartShop.getMoney(),cartShop.getId(),cartShop.getPid()};
        return JdbcTemplete.DMLTemplete(sql,arrs);
    }

    @Override
    public List<Cart> selectById(Integer id) {
        String sql = "select * from tb_cart where id = ?";
        return JdbcTemplete.DQLTemplete(sql,new ResultHandlerAsListImpl<>(Cart.class),id);
    }

    @Override
    public int deleteByUnionPrimary(Integer id, int pid) {
        String sql = "delete from tb_cart where id = ? and pid = ?";
        return JdbcTemplete.DMLTemplete(sql,id,pid);
    }

    @Override
    public int deleteById(Integer id) {
        String sql = "delete from tb_cart where id = ?";
        return JdbcTemplete.DMLTemplete(sql,id);
    }

    @Override
    public int deleteByids(List ids) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < ids.size(); i++) {
            if(i == ids.size() - 1){
                sb.append("?");
            }else{
                sb.append("?,");
            }
        }
        String sql = "delete from tb_cart where pid in ("+sb.toString()+")";
        return JdbcTemplete.DMLTemplete(sql,ids.toArray());
    }
}
